package com.ushine.web.base.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: yls
 * @Date 2020/12/30 12:00
 * @Description: 读写文件
 * @Version 1.0
 **/
@Slf4j
public class FileUtils {
    /**
     * 功能描述:
     * 〈 按行读取文件 〉
     *
     * @param path 文件路径
     * @return : void
     * @throws IOException io异常
     * @author : yls
     * @date : 2022/4/11 11:12
     */
    public static Map<String, String> readFile(String path) throws IOException {
        Map<String, String> maps = new HashMap<>();
        BufferedReader br = null;
        String line = null;
        File fin = null;
        try {
            fin = new File(path);
            br = new BufferedReader(new FileReader(fin));
            while ((line = br.readLine()) != null) {
                String[] split = line.split("=", -1);
                if (split.length < 1) {
                    continue;
                }
                if (split.length == 1){
                    maps.put(split[0], split[0]);
                }
                maps.put(split[0], split[1]);

            }
        } finally {
            if (br != null) {
                br.close();
            }
        }
        return maps;
    }

    /**
     * 功能描述:
     * 〈 写入文件 〉
     *
     * @param path     文件路径
     * @param contents 内容集合
     * @return : void
     * @throws IOException io异常
     * @author : yls
     * @date : 2020/12/30 12:02
     */
    public static void writeFile(String path, List<String> contents) throws IOException {
        BufferedWriter bw = null;
        File fin = null;
        try {
            fin = new File(path);
            bw = new BufferedWriter(new FileWriter(fin));
            for (int i = 0; i < contents.size(); i++) {
                bw.write(contents.get(i));
                bw.newLine();
            }
        } finally {
            if (bw != null) {
                bw.close();
            }
        }
    }

    /**
     * 功能描述:
     * 〈 获取项目的根路径 〉
     *
     * @return : java.lang.String
     * @throws IOException io异常
     * @author : yls
     * @date : 2022/4/12 13:34
     */
    public static String getProjectPath() throws IOException {
        File file = new File("");
        String filePath = file.getCanonicalPath();
        return filePath;
    }

    /**
     * 功能描述:
     * 〈 获取测试文件路径 〉
     *
     * @return : java.lang.String
     * @throws IOException io异常
     * @author : yls
     * @date : 2022/4/12 13:38
     */
    public static String getTestResourcePath() {
        String projectPath = "";
        try {
            projectPath = getProjectPath();
        } catch (IOException e) {
            log.info("获取测试资源路径异常：", e);
        }
        return projectPath + "\\src\\test\\resource";
    }


    /**
     * 功能描述:
     * 〈 生成组合公式 〉
     *
     * @param
     * @return : void
     * @throws Exception
     * @author : yls
     * @date : 2022/4/11 11:05
     */
    private static void createFormula() {
        //=CONCATENATE(IF(C7="⚪","1",""),",",IF(D7="⚪","1",""))
        StringBuilder sb = new StringBuilder("=CONCATENATE(B7,\"=\",");
        for (char c = 'C'; c < 'W'; c++) {
            if (c == 'W' - 1) {
                sb.append("IF(" + c + "7=\"⚪\",\"1\",\"\")");
            } else {
                sb.append("IF(" + c + "7=\"⚪\",\"1\",\"\"),\",\",");
            }

        }
        sb.append(")");
        System.out.println(sb.toString());
    }

    /**
     * 功能描述:
     * 〈 生成组合公式 〉
     *
     * @param
     * @return : void
     * @throws Exception
     * @author : yls
     * @date : 2022/4/11 11:05
     */
    private static void createFormula2() {
        //=CONCATENATE(IF(C7="⚪","1",""),",",IF(D7="⚪","1",""))
        StringBuilder sb = new StringBuilder("=CONCATENATE(B3,\"=\",");
        for (char c = 'C'; c <= 'Z'; c++) {
            sb.append(c + "3,\",\",");
        }
        for (char c = 'A'; c <= 'G'; c++) {
            if (c == 'G') {
                sb.append("A" + c + "3");
            } else {
                sb.append("A" + c + "3,\",\",");
            }

        }
        sb.append(")");
        System.out.println(sb.toString());
    }


    public static void main(String[] args) {

    }

}
